﻿using System;
using System.Collections.Generic;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Shapes;
using QLInternet.ViewModel;
using QLInternet.Connection;
using QLInternet.Model;
using System.Collections.ObjectModel;
namespace QLInternet
{

    public partial class FmKhachHang : Window
    {
        public String mahd = "";
        public String makh = "";
        private DangKy dk;
        private Query qr;
        public CustomerViewModel model;
        public Connect conn;
        public ObservableCollection<CustomerViewModel> customer;
        public bool isClosed = false;
        public FmKhachHang()
        {
            this.InitializeComponent();
            qr = new Query();
            dk = new DangKy();
            model = new CustomerViewModel();
            customer = new ObservableCollection<CustomerViewModel>();
        }

        private void Window_Loaded(object sender, RoutedEventArgs e)
        {
            init();
            btnUpdate.IsEnabled = true;
        }

        private void NumericTextbox(object sender, KeyEventArgs e)
        {
            if ((int)e.Key >= 43 || (int)e.Key <= 34)//ky tu # so
            {
                if ((int)e.Key != 2)
                    e.Handled = true;
                else
                    e.Handled = false;

            }
            else
                e.Handled = false;
        }

        public void init()
        {
            txtHotenKh.Text = model.HoTen;
            txtMaKh.Text = model.MaKh + "";
            txtNgheNghiep.Text = model.NgheNghiep;
            txtEmailKh.Text = model.Email;
            txtDiaChiKh.Text = model.DiaChi;
            txtDCCDKh.Text = model.DiaChiCaiDat;
            txtDiaChiHoaDonKh.Text = model.DiaChiHoaDon;
            txtDienThoaiKh.Text = model.DienThoai + "";
            txtSoLuongTaiKhoan.Text = model.SoLuongTaiKhoan + "";
            txtCmndKh.Text = model.CMND + "";
            if (cbbxGoiCuocKh.Items.Count == 0)
                dk.loadDataToComboBox(conn, cbbxGoiCuocKh, qr.sqlGoiCuoc);
            cbbxGoiCuocKh.Text = model.TenGoiCuoc;
            if (cbbxKieuCaiDatKh.Items.Count == 0)
                dk.loadDataToComboBox(conn, cbbxKieuCaiDatKh, qr.sqlKieuCaiDat);
            cbbxKieuCaiDatKh.Text = model.TenKieuCaiDat;
            if (cbbxTinhTrangKh.Items.Count == 0)
                dk.loadDataToComboBox(conn, cbbxTinhTrangKh, qr.sqlTinhTrangSuDung);
            cbbxTinhTrangKh.Text = model.TenTinhTrangSuDung;
            if (cbbxPhuongThucKh.Items.Count == 0)
            {
                cbbxPhuongThucKh.Items.Add("Trọn gói");
                cbbxPhuongThucKh.Items.Add("Phương thức lưu lượng");
            }
            cbbxPhuongThucKh.Text = model.TronGoi;
            if (cbbxMoDemKh.Items.Count == 0)
                dk.loadDataToComboBox(conn, cbbxMoDemKh, qr.queryModem);
            cbbxMoDemKh.Text = model.TenModem;
        }

        private void tabCtrl_SelectionChanged(object sender, SelectionChangedEventArgs e)
        {
            if (tabCtrl.SelectedIndex < 0)
                return;
            if (tabThanhToanPhiHoaMang.IsSelected)
            {
                customer = dk.disPlayBillInfomation(conn, qr.thongTinThanhToan + model.MaKh);
                DataGridThongTinThanhToan.ItemsSource = customer;
            }
            if (tabBaoLoi.IsSelected)
            {
                String sql = qr.getMaBaoLoi + model.MaHopDong + " and hopdong.makhachHang = " + model.MaKh;
                int maBaoLoi = dk.getId(conn, sql);
                customer = dk.disPlayErrorInfomation(conn, qr.thongTinLoi + maBaoLoi);
                if (customer != null)
                    DataGridDanhSachLoi.ItemsSource = customer;
            }
        }

        private void btnUpdate_Click(object sender, RoutedEventArgs e)
        {
            String loaiPhuongThuc = "";
            if (cbbxPhuongThucKh.SelectedItem.ToString().Trim().Equals("Trọn gói"))
                loaiPhuongThuc = "1";
            else
                loaiPhuongThuc = "0";
            int slTaiKhoan = 0;
            if (!txtSoLuongTaiKhoan.Text.Equals(""))
                slTaiKhoan = Int32.Parse(txtSoLuongTaiKhoan.Text);
            else
                slTaiKhoan = 0;
            model = new CustomerViewModel 
            {
                MaKh = Int32.Parse(txtMaKh.Text),
                MaHopDong = Int32.Parse(mahd),
                HoTen = txtHotenKh.Text,
                NgheNghiep = txtNgheNghiep.Text,
                DiaChi = txtDiaChiKh.Text,
                DiaChiCaiDat = txtDCCDKh.Text,
                DiaChiHoaDon = txtDiaChiHoaDonKh.Text,
                TenGoiCuoc = ((ComboboxItem)cbbxGoiCuocKh.SelectedItem).Id + "",
                Email = txtEmailKh.Text,
                TenTinhTrangSuDung = ((ComboboxItem)cbbxTinhTrangKh.SelectedItem).Id + "",
                DienThoai = Int32.Parse(txtDienThoaiKh.Text),
                CMND = Int32.Parse(txtCmndKh.Text),
                SoLuongTaiKhoan = slTaiKhoan,
                TenNguoiSuDung = txtNguoiSuDungKh.Text,
                TronGoi = loaiPhuongThuc,
                TenModem = ((ComboboxItem)cbbxMoDemKh.SelectedItem).Id + "",
                TenKieuCaiDat = ((ComboboxItem)cbbxKieuCaiDatKh.SelectedItem).Id + ""
            };

            String queryHoten = qr.checkTenKh + model.HoTen + "' and makhachhang = " + model.MaKh;
            String queryCMND = qr.checkCmnd + model.CMND + "' and makhachhang = " + model.MaKh;
            String queryDienThoai = qr.checkDienThoai + model.DienThoai + "' and makhachhang = " + model.MaKh;
            String queryDiaChi = qr.checkDiachi + model.DiaChi + "' and makhachhang = " + model.MaKh;
            String queryEmail = qr.checkEmail + model.Email + "' and makhachhang = " + model.MaKh;
            String queryNgheNGhiep = qr.checkNgheNghiep + model.NgheNghiep + "' and makhachhang = " + model.MaKh;

            String queryDiaChiCaiDat = qr.checkDccd + model.DiaChiCaiDat + "' and makhachhang = " + model.MaKh;
            String queryDiaCHiHoaDon = qr.checkDchd + model.DiaChiHoaDon + "' and makhachhang = " + model.MaKh;
            String querySLTaiKhoan = qr.checkSltk + model.SoLuongTaiKhoan + "' and makhachhang = " + model.MaKh;
            String queryTenNguoiSuDung = qr.checktenNguoiSuDung + model.TenNguoiSuDung + "' and makhachhang = " + model.MaKh;
            String queryPhuongThuc = qr.checkPhuongThuc + Int32.Parse(model.TronGoi) + "' and makhachhang = " + model.MaKh;
 

            String sqlmctcd = "select MaChiTiet from CHITIETKIEUCAIDAT where MaKieuCaiDat = '" + Int32.Parse(model.TenKieuCaiDat) + "' and MaModem = '" + Convert.ToInt32(model.TenModem) + "'";
            int maCtkcd = dk.getId(conn, sqlmctcd);

            String queryMaChiTietKieuCaiDat = qr.checkMactkcd + maCtkcd + "' and makhachhang = " + model.MaKh;
            String queryTinhTrangSuDung = qr.checkTinhTrangSuDung + Int32.Parse(model.TenTinhTrangSuDung) + "' and mahopdong = " + model.MaHopDong;
            if (dk.isExist(conn, queryHoten) && dk.isExist(conn, queryCMND) && dk.isExist(conn, queryNgheNGhiep) && dk.isExist(conn, queryEmail) &&
                dk.isExist(conn, queryDienThoai) && dk.isExist(conn, queryDiaChi) && dk.isExist(conn, queryDiaChiCaiDat) && dk.isExist(conn, queryDiaCHiHoaDon) &&
                dk.isExist(conn, queryEmail) && dk.isExist(conn, querySLTaiKhoan) && dk.isExist(conn, queryTenNguoiSuDung) && dk.isExist(conn, queryPhuongThuc) &&
                dk.isExist(conn, queryMaChiTietKieuCaiDat) && dk.isExist(conn,queryTinhTrangSuDung))
            {
                return;
            }

            dk.capNhatThongTinKh(conn, model);
            MessageBox.Show("Cập nhật thành công !!! ","Thông báo",MessageBoxButton.OK,MessageBoxImage.Information);
        }


        private void Window_Closed(object sender, EventArgs e)
        {
            isClosed = true;
        }  

    }
}